package com.cactus.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cactus.entity.UserAdd;
import com.cactus.service.UserAddServer;
import com.cactus.utils.JsonResult;
import com.cactus.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户收货地址接口
 */
@RestController
@RequestMapping("/userAdd")
public class UserAddController {

    @Autowired
    private UserAddServer userAddServer;

    @PostMapping
    public JsonResult edit(@RequestBody UserAdd userAdd) {
        String userId = SecurityUtils.getCurrentUsername().getUid();
        UserAdd findAdd = userAddServer.getAddIdByUser(userId);
        if (findAdd == null) {
            userAdd.setId(IdWorker.getId() + "");
            userAddServer.save(userAdd);
        } else {
            userAdd.setId(findAdd.getId());
            userAddServer.updateById(userAdd);
        }
        return JsonResult.ok();
    }


    @GetMapping
    public JsonResult findByPage() {
        String uid = SecurityUtils.getCurrentUsername().getUid();
        return JsonResult.ok(userAddServer.getAddIdByUser(uid));
    }


}
